package com.atguigu.crowd.config;


import com.atguigu.crowd.exception.AccessForbiddenException;
import com.atguigu.crowd.exception.BaseException;
import com.atguigu.crowd.exception.LoginFailedException;
import com.atguigu.crowd.util.Constant;
import com.atguigu.crowd.util.CrowdUtil;
import com.atguigu.crowd.util.ResultEntity;
import com.google.gson.Gson;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//表明这是个全局异常处理类
@ControllerAdvice
public class CrowdExceptionResolver {

    //算术异常处理映射
    @ExceptionHandler(value = ArithmeticException.class)
    public ModelAndView resolveNullPointException(
            ArithmeticException exception, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String viewName = "system_error";
        return commonResolve(exception, request, response, viewName);
    }

    //登录失败异常处理映射
    @ExceptionHandler(value = LoginFailedException.class)
    public ModelAndView resolveLoginFailedException(
            LoginFailedException exception, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String viewName = "admin-login";
        return commonResolve(exception, request, response, viewName);
    }

    //访问禁止异常处理映射
    @ExceptionHandler(value = AccessForbiddenException.class)
    public ModelAndView resolveAccessForbiddenException(
            AccessForbiddenException exception, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String viewName = "admin-login";
        return commonResolve(exception, request, response, viewName);
    }

    //普通异常返回页面
    @ExceptionHandler(value = BaseException.class)
    public ModelAndView resolveBaseException(
            BaseException exception, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String viewName = "system_error";
        return commonResolve(exception, request, response, viewName);
    }

    /**
     * 通用异常处理
     *
     * @param exception 异常类型
     * @param request   请求
     * @param response  响应
     * @param viewName  返回的视图名称
     * @return 结果
     * @throws IOException 抛出异常
     */
    private ModelAndView commonResolve(Exception exception, HttpServletRequest request, HttpServletResponse response, String viewName) throws IOException {
        //1.先判断请求类型
        Boolean aBoolean = CrowdUtil.judgeRequestType(request);
        System.out.println(aBoolean);

        if (aBoolean) {
            //如果是ajax请求
            ResultEntity<Object> resultEntity = ResultEntity.failed(exception.getMessage());
            //转json
            Gson gson = new Gson();
            String result = gson.toJson(resultEntity);
            //作出响应
            response.getWriter().write(result);
            return null;
        } else {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject(Constant.ATTR_NAME_EXCEPTION, exception.getMessage());
            modelAndView.setViewName(viewName);
            return modelAndView;
        }
    }
}
